
class Api{
  // 初始化
  constructor () {
    this.Mock = require('mock.js');
    this.Random = this.Mock.Random;
    this.res = '';
  }
  // 接口数据模拟
  ajax(data = '', fn, method = "get", header = {}) {
    let api = data;
    // 首页综合数据模拟
    if (api === 'index') { 
      this.res = this.Mock.mock({
        'swiper|4': [
          this.Random.image('200x100', '#333', '#FFF', 'png', '!')
        ],
        'tab|7': [
          {
            'id|+1': 0,
            'style': this.Random.cname(),
            'content|10': [
              {
                'id|+1':0,
                'img': this.Random.image('200x100', '#333', '#FFF', 'png', '!'),
                'title': this.Random.cparagraph(7, 22),
                'price': 199,
                'count': 2204
              }
            ] 
          }
        ],
        'lowPrice|10': [
            {
            'img': this.Random.image('200x100', '#333', '#FFF', 'png', '!'),
              'stock_num': '888',
              'price': '999'
            }
        ],
        'propInfo|3': [
            {
            'img': this.Random.image('200x100', '#333', '#FFF', 'png', '!'),
              'hot_num': '90'
            }
          ],
          'scrollNoTag|10':[
            {
              'img': this.Random.image('200x100', '#333', '#FFF', 'png', '!'),
              'price': '20',
              'initial': '20',
              'name': this.Random.cparagraph(6, 8)
            }
          ]
      });
    }

    // 相关推荐分类
    if (api === 'recommend') {
      this.res = this.Mock.mock({
        'recommend|7': [
          {
            'id|+1': 0,
            'name': this.Random.cname()
          }
        ]
      });
    }

    // 获取推荐分类第一个的内容
    if (api === 'recommendContent') {
      this.res = this.Mock.mock({
        'content|10': [
          {
            'id|+1': 0,
            'img': this.Random.image('200x100', '#333', '#FFF', 'png', '!'),
            'title': this.Random.cparagraph(7, 22),
            'price': 199,
            'count': 2204
          }
        ]
      });
    }
    
    // 分类
    if (api === 'soft') {
      this.res = this.Mock.mock({
        'common|7': [
          {
            'img': this.Random.image('200x100', '#333', '#fff', 'png', '!'),
            'title': this.Random.cname()
          }
        ],
        'hot|11': [
          {
            'name': this.Random.cname()
          }
        ]
      });
    }

    // 搜索关键词
    if (api === 'searchKeyWord') {
      this.res = this.Mock.mock({
        'keyword|10': [
          {
            'name': this.Random.cname()
          }
        ]
      });
    }

    // 搜索结果
    if (api === 'result') {
      this.res = this.Mock.mock({
        'result|7': [
          {
            'id|+1': 0,
            'img': this.Random.image('200x100', '#333', '#FFF', 'png', '!'),
            'title': this.Random.cparagraph(7, 22),
            'price': 199,
            'count': 2204
          }
        ]
      });
    }

    // 商品详情页
    if (api === 'detail') {
      this.res = this.Mock.mock({
        'swiper|6': [
          this.Random.image('200x100', '#333', '#FFF', 'png', '!')
        ]
      });
    }
    // 输出结果
    // console.log(JSON.stringify(res, null, 2))
    fn(this.res);
  }

}

export { Api }